package ciyuan.Gui.Bar;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.List;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class HealthBar extends AbstractGui {

    private static long lastTime=0;
    private static LivingEntity target;
    private static volatile HealthBar INSTANCE;

    @SubscribeEvent
    public static void onOverlayRender(RenderGameOverlayEvent event) {
        if (event.getType() != RenderGameOverlayEvent.ElementType.ALL) {
            return;
        }
        if (Minecraft.getInstance().player == null) {
            return;
        }

        if (System.currentTimeMillis() - lastTime > 500) {
            PlayerEntity playerEntity = Minecraft.getInstance().player;
            target = getLookingAt(playerEntity);
            lastTime = System.currentTimeMillis();
        }

        if (INSTANCE == null) {
            synchronized (HealthBar.class) {
                if (INSTANCE == null) {
                    INSTANCE = new HealthBar();
                }
            }
        }

        if (target != null && !target.equals(Minecraft.getInstance().player)) {
            HealthBar healthBar = INSTANCE;
            healthBar.matrixStack = event.getMatrixStack();
            healthBar.render(target);
        }

    }

    private static LivingEntity getLookingAt(PlayerEntity playerEntity) {
        int scanDistance = 16;
        LivingEntity maxCosEntity = null;
        double maxCos = 0;
        AxisAlignedBB axisAlignedBB = new AxisAlignedBB(playerEntity.getX() - scanDistance, playerEntity.getY() - scanDistance, playerEntity.getZ() - scanDistance, playerEntity.getX() + scanDistance, playerEntity.getY() + scanDistance, playerEntity.getZ() + scanDistance);
        List<LivingEntity> livingEntityList = playerEntity.level.getEntitiesOfClass(LivingEntity.class, axisAlignedBB, null);
        for (LivingEntity livingEntity : livingEntityList) {
            Vector3d playerViewVector = playerEntity.getViewVector(1.0F).normalize();
            Vector3d playerToTargetVector = new Vector3d(livingEntity.getX() - playerEntity.getX(), livingEntity.getEyeY() - playerEntity.getEyeY(), livingEntity.getZ() - playerEntity.getZ());
            playerToTargetVector = playerToTargetVector.normalize();
            double cos = playerViewVector.dot(playerToTargetVector);
            if (cos < 0) cos = -cos;
            if (cos > maxCos && cos > 0.9396 && playerEntity.canSee(livingEntity)) {
                maxCosEntity = livingEntity;
                maxCos = cos;
            }
        }
        return maxCosEntity;
    }

    private static final Minecraft minecraft = Minecraft.getInstance();
    private static final ResourceLocation HUD = new ResourceLocation("ciyuanrpg", "textures/gui/guaihealth.png");
    private static final ResourceLocation HUD2 = new ResourceLocation("ciyuanrpg", "textures/gui/guaihealth2.png");
    private MatrixStack matrixStack;

    public void render(LivingEntity livingEntity) {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        int textureWidth = 128;
        int textureHeight = 40;
        double percentage = livingEntity.getHealth() / livingEntity.getMaxHealth();
        if (percentage > 1) {
            percentage = 1;
        }
        minecraft.getTextureManager().bind(HUD2);
        blit(matrixStack, 0, 0, 0, 0, (int) (textureWidth * percentage), textureHeight, (int) (textureWidth * percentage), textureHeight);
        minecraft.getTextureManager().bind(HUD);
        blit(matrixStack, 0, 0, 0, 0, textureWidth, textureHeight, textureWidth, textureHeight);
        StringTextComponent healthNumber = new StringTextComponent(livingEntity.getDisplayName().getString());
        drawCenteredString(matrixStack, minecraft.font, healthNumber, 50, 10, 16777215);
        StringTextComponent healthNumber2 = new StringTextComponent((int) livingEntity.getHealth() + "/" + (int) livingEntity.getMaxHealth());
        drawCenteredString(matrixStack, minecraft.font, healthNumber2, 60, 20, 16777215);
    }

}
